//
//  YTUserCenterController.m
//  YUNT
//
//  Created by haha on 2017/11/11.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTUserCenterController.h"
#import "YTLoginController.h"
#import "YTUserSettingController.h"
#import "YTAboutUsController.h"
#import "YTAnswerController.h"

@interface YTUserHeaderCell ()
@property (weak, nonatomic) IBOutlet UIButton *iconButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@end

@implementation YTUserHeaderCell

@end

@interface YTUserContentCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@end

@implementation YTUserContentCell

@end

@interface YTUserCenterController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation YTUserCenterController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.navigationController.navigationBar setHidden:YES];
    
    [self.mainTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mainTableView.tableFooterView = [[UIView alloc] init];
    self.mainTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0)
    {
        
    }
    else
    {
        if (indexPath.row == 0)
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YTUserSetting" bundle:[NSBundle mainBundle]];
            
            YTAboutUsController *aboutVC = (YTAboutUsController *)[storyboard instantiateViewControllerWithIdentifier:@"YTAboutUsController"];
            
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
        else if (indexPath.row == 1)
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YTUserSetting" bundle:[NSBundle mainBundle]];
            
            YTAnswerController *answerVC = (YTAnswerController *)[storyboard instantiateViewControllerWithIdentifier:@"YTAnswerController"];
            
            [self.navigationController pushViewController:answerVC animated:YES];
        }
        else if (indexPath.row == 2)
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YTUserSetting" bundle:[NSBundle mainBundle]];
            
            YTUserSettingController *settingVC = (YTUserSettingController *)[storyboard instantiateViewControllerWithIdentifier:@"YTUserSettingController"];
            
            [self.navigationController pushViewController:settingVC animated:YES];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 0.0f;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 10.0f;
    }
    return 0.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 10.0f;
    }
    return 0.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 210.0f;
    }
    else
    {
        return 60.0f;
    }
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 10)];
    
    [headerView setBackgroundColor:[UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0]];
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0)
    {
        YTUserHeaderCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"mine_header" forIndexPath:indexPath];
        
        cell.iconImageView.layer.masksToBounds = YES;
        
        cell.iconImageView.layer.cornerRadius = 32;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    else
    {
        YTUserContentCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"mine_content" forIndexPath:indexPath];
        if (indexPath.row == 0)
        {
            cell.contentLabel.text = Language(@"关于我们");
            cell.iconImageView.image = [UIImage imageNamed:@"my_expression"];
        }
        else if (indexPath.row == 1)
        {
            cell.contentLabel.text = Language(@"常见问题");
            cell.iconImageView.image = [UIImage imageNamed:@"my_teamImage"];
        }
        else if (indexPath.row == 2)
        {
            cell.contentLabel.text = Language(@"设置");
            cell.iconImageView.image = [UIImage imageNamed:@"my_seting"];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (IBAction)iconButtonAction:(UIButton *)sender
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *first = [UIAlertAction actionWithTitle:Language(@"从手机中选择") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self p_openPhotoLibrary];
    }];
    
    UIAlertAction *second = [UIAlertAction actionWithTitle:Language(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self p_openCamera];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:Language(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:first];
    
    [alert addAction:second];
    
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (IBAction)loginButtonAction:(UIButton *)sender
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YTLoginStoryBoard" bundle:[NSBundle mainBundle]];
    
    YTLoginController *loginVC = (YTLoginController *)[storyboard instantiateViewControllerWithIdentifier:@"YTLoginController"];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    
    [self presentViewController:navi animated:YES completion:nil];
}

- (void)p_openCamera
{
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        return;
    }
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController presentViewController:picker animated:YES completion:nil];
    
}

- (void)p_openPhotoLibrary
{
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    
    pickerImage.delegate = self;
    
    pickerImage.allowsEditing = YES;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        
    }
    pickerImage.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController presentViewController:pickerImage animated:YES completion:nil];
}

#pragma mark - PickerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        image = [info objectForKey:UIImagePickerControllerEditedImage];
        //图片存入相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
   
    //todo:上传保存
//    NSData *imageData = [FileManager compressImage:image];

    YTUserHeaderCell *cell = [self.mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    cell.iconImageView.image = image;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 点击cancel 调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
